-- BraveItem
-- Created by chengb Mar/30/2016
-- 勇者斗恶龙中的物品格子

-- 初始化
function initBraveItem(self)
    self.item:removeAllChildren();

    local buildingInfo = BuildingBonusM.queryBuildingView(self.gridData.class);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 有监狱图标，需要击杀所有怪物
            if self.gridData.need_prison == 1 then
                -- 如果还有怪没死
                if not DungeonM.isAllMonsterDead() then
                    if BraveVSDragonM.isInSwampCave() then
                        -- 沼泽洞窟中
                        alert(getLocStr("swamp_cave_pick_tip"));
                    else
                        alert(getLocStr("pick_up_instance_target_fail"));
                    end
                    return;
                end
            end

            if type(buildingInfo) == "table" then
                -- 配置了建筑查看
                local function okFunc()
                    DungeonActionM.go("brave_building_bonus", self.gridData:getPos());
                end

                AudioM.playFx("ui_open1");

                if self.gridData.building_view2 then
                    -- 两个按钮的建筑查看界面
                    self:openBuildingView2(okFunc);
                else
                    -- 打开建筑查看界面
                    self:openBuildingView(okFunc);
                end
            else
                DungeonActionM.go("brave_building_bonus", self.gridData:getPos());
            end
        end
    end

    local icon = self.gridData.icon;
    if not icon and type(buildingInfo) == "table" then
        icon = buildingInfo["icon"];
    end

    local iconPath = getLevelItemIconPath(icon);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);

    -- 发光特效偏移
    self.shiningAjustPos = cc.p(70, 100);
    if self.gridData.need_shining == 1 then
        self.needShining = true;
        shiningEffect(self.icon, self.shiningAjustPos.x, self.shiningAjustPos.y);
    end

    -- 配置了监狱图标
    if self.gridData.need_prison == 1 then
        -- 需要用监狱图标遮挡
        local prisonIcon = getLevelItemIconPath("prison");
        local prisonImg  = ccui.ImageView:create(prisonIcon);
        prisonImg:setName("prison_icon");
        self.item:addChild(prisonImg);
    end

    if BraveVSDragonM.hasGotBuildingBonus(self.gridData.class) then
        -- 奖励已被领取，格子不可见
        self.icon:setVisible(false);
    else
        self.icon:setVisible(true);
    end
end
